<template>
	<view class="login">
		<view class="head_img"><img src="../../static/image/toxiang.png" class="img" /></view>
		<view class="content">
			<input type="number" placeholder="手机号" v-model="phone" />
			<input type="password" placeholder="密码" v-model="pwd" />
			<img src="../../static/image/denglu.png" class="denglu" @click="goLogin" />
			<view class="blue" @click="codeLogin">验证码登录</view>
			<view class="register" @click="register">没有账号？去注册</view>
		</view>
	</view>
</template>
<script>
import { mapActions } from 'vuex';
export default {
	data() {
		return {
			phone: '',
			pwd: '',
			img: '../../static/image/beijing.png'
		};
	},
	onLoad() {

	},
	methods: {
			...mapActions(['login']),
			//验证码登录
			codeLogin() {
				uni.navigateTo({
					url: '../codeLogin/codeLogin'
				})
			},
			register() {
				uni.navigateTo({
					url: '../register/register'
				})
			},
			goLogin() {
				if (!this.phone) {
					this.$api.msg('请输入账号');
					return;
				}
				if (!this.pwd) {
					this.$api.msg('请输入密码');
					return;
				}
				var obj = {
					account: this.phone,
					password: this.pwd
				}
				// const { username, password } = this;
				// sendData = { username, password}
				this.login(obj).then(res => {
						if (res.data.code == 1) {
							this.$common.requests('/api/user/is_news_notice').then(res => {
								uni.setStorageSync('yinstus', res.data.data);
							})
							// return false;
							/////////////发送
							var jsonstr = {
								uid: res.data.data.userinfo.id,
								type: "tologin1"
							}
							var arrstr = JSON.stringify(jsonstr);
							uni.sendSocketMessage({
								data: arrstr
							});
							/////////////接受
							var innerAudioContext = uni.createInnerAudioContext();
							uni.onSocketMessage(function(res) {
								var jsonmsg = JSON.parse(res.data);
								if (jsonmsg.emit == 'chatMessage' || jsonmsg.emit == 'addfriend' && uni.getStorageSync('yinstus') == 1) {
									innerAudioContext.autoplay = true;
									innerAudioContext.src = 'static/image/yin.mp3';
									innerAudioContext.play();
								}
								console.log('收到服务器内容：' + jsonmsg.emit);
								console.log('收到服务器内容：' + res.data);


							});
							this.$api.msg("登陆成功");
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/message/message'
								})
							}, 800)
						}else{
							this.$api.msg(res.data.msg)
						}

				})
				// 	this.logining = false
				// 	this.getCartProducts()
				// 	if (this.referer) {
				// 		uni.navigateTo({
				// 			url: decodeURIComponent(this.referer),
				// 			fail: this.toCenter(),
				// 		})
				// 	} else {
				// 		this.toCenter()
				// 	}
				// }).catch(e => {
				// 	this.logining = false
				// })
			}

			// const { username, password } = this;
			// sendData = { username, password}

			// 	this.logining = false
			// 	this.getCartProducts()
			// 	if (this.referer) {
			// 		uni.navigateTo({
			// 			url: decodeURIComponent(this.referer),
			// 			fail: this.toCenter(),
			// 		})
			// 	} else {
			// 		this.toCenter()
			// 	}
			// }).catch(e => {
			// 	this.logining = false
			// })
		}
};
</script>

<style scoped>
.login {
	height: 100vh;
}

.all {
	padding: 0;
	margin: 0;
	font-size: 32upx;
	color: #333333;
}

.head_img {
	padding: 160upx 0 10upx 0;
	text-align: center;
}

.img {
	width: 110px;
	height: 110px;
}

.content {
	padding: 30upx 40upx;
	text-align: center;
}

.content input {
	height: 100upx !important;
	margin: 20upx 0;
	text-align: left;
	border-bottom: 1px solid #eaeaea !important;
}

.denglu {
	width: 100%;
	height: 80upx;
	margin: 30upx 0 20upx 0;
}

.blue {
	color: #4c68ff;
	letter-spacing: 1px;
}

.register {
	width: 100%;
	position: absolute;
	bottom: 20px;
	left: 0;
}
</style>
